<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box"> </div>
      <div class="login_title">
        论坛
      </div>
      <el-form :model="LoginForm"
               :rules="login_form"
               class="login_form"
               ref="resetLoginForm">
        <el-form-item prop="username">
          <el-input v-model="LoginForm.user_name"
                    placeholder="请输入用户名"
                    prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="LoginForm.user_password"
                    placeholder="请输入密码"
                    prefix-icon="iconfont icon-3702mima"
                    type="password"></el-input>
        </el-form-item>

        <el-form-item>
          <div class="buttonBox">
            <div class="b1">
              <span>还没有账号？</span>
              <el-button  type="primary" plain
                         @click="to_register">注册一下</el-button>
            </div>
            <div class="btns">
              <el-button type="primary"
                         @click="login">登录</el-button>
              <el-button type="info"
                         @click="resetLoginForm">重置</el-button>
            </div>

          </div>
        </el-form-item>
      </el-form>
    </div>
   
  </div>
</template>

<script>
import { userLogin } from "@/api";
import {searchCollectionByUserId} from '../../api'
export default {

  data () {
    return {
      isShow4: false,
      isShow5: false,
      LoginForm: {
        user_name: '',
        user_password: ''

      },
      login_form: {
        username: [
          { required: false, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: false, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到16个字符之间', trigger: 'blur' }
        ]
      },
      user_temp:{},
      user:{},
      collection:[]
    }
  },
  created () {
   
    // console.log("register:",this.$store.state.registeruser);
    // if(this.$store.state.registeruser != null){
    //   this.LoginForm.username = this.$store.state.registeruser.user_name;
    //   this.LoginForm.password = this.$store.state.registeruser.password;
    // }
    
  },
  methods: {
    resetLoginForm () {
      this.$refs.resetLoginForm.resetFields()
    },
    login () {
      // if (this.$data.LoginForm.username !== 'admin' || this.$data.LoginForm.password !== '123456') return this.$message.error('登录失败')
    let user_name = this.LoginForm.user_name
    let user_password = this.LoginForm.user_password
    let that = this
      userLogin(user_name, user_password)
        .then(res => {
          const { data } = res.data;
          this.user = data;
          console.log(data);
          if (data != null) {
            this.$store.dispatch("aLogin", {
              user: data,
              message: "",
              success: () => {
                that.$router.push('/forumHome')
                console.log("欢迎您");
                    
              }
            });
          } else {
            alert("该用户不存在");
          }
          this.$router.go(-1);
        })
        .catch(() => {});
    },
    to_register(){
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="less" scoped>

.login_container {
  height: 721px;
  background-color: #2b4b6b;
  background-image: url(../../assets/images/login_bg.png);
  // background-size: 100%;
  
.login_box {
  background-color: #fff;
  opacity:0.75;
  width: 560px;
  height: 300px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    width: 130px;
    height: 130px;
    //border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    padding: 10px;
    left: 15%;
    transform: translate(-50%, -60%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login_title {
    position: absolute;
    left: 30%;
    font-size: 30px;
    transform: translate(3%, 0%);
    //border: 1px solid #eee;
    //box-shadow: 0 0 10px #ddd;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.buttonBox {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .b1 {
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
}
</style>
